package web.common.quartz;

import java.util.Enumeration;
import java.util.Properties;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.IPlugin;

import cn.dreampie.PropertiesKit;
import kit.ToolUtil;

/**
 * Created by wangrenhui on 14-4-21.
 */
public class QuartzPlugin implements IPlugin {

	private Logger logger = LoggerFactory.getLogger(getClass());
	private static ScheduleJobUtils scheduleJobUtils = ScheduleJobUtils.me;
	public static String dsName = "main";
	public static boolean dsAlone = false;
	/**
	 * 默认配置文件*
	 */
	private String config = "quartz/quartz.properties";

	private String jobs = "quartz/jobs.properties";

	public QuartzPlugin() {

	}

	public QuartzPlugin(String dsName) {
		this.dsName = dsName;
	}

	public boolean start() {
		try {
			// 加载配置文件
			Properties properties = PropertiesKit.me().loadPropertyFile(config);
			// 实例化
			QuartzKit.setSchedulerFactory(new StdSchedulerFactory(properties));
			// 获取Scheduler
			Scheduler sched = QuartzKit.getSchedulerFactory().getScheduler();
			// 内存,数据库的任务
			sched.start();
			// 属性文件中的任务
			startPropertiesJobs();
			return true;
		} catch (Exception e) {
			throw new RuntimeException("Can't start quartz plugin.", e);
		}
	}

	public boolean stop() {
		try {
			QuartzKit.getSchedulerFactory().getScheduler().shutdown();
			QuartzKit.setSchedulerFactory(null);
			return true;
		} catch (Exception e) {
			throw new RuntimeException("Can't stop quartz plugin.", e);
		}
	}

	public void startPropertiesJobs() {
		if (PropertiesKit.exist(jobs)) {
			Properties properties = PropertiesKit.me().loadPropertyFile(jobs);
			Enumeration enums = properties.keys();

			while (enums.hasMoreElements()) {
				String key = enums.nextElement() + "";
				if (!key.startsWith("job")) {
					continue;
				}

				String[] keyArr = key.split("\\.");

				String jobClassKey = key.replace(keyArr[2], "class");
				String nameKey = key.replace(keyArr[2], "name");
				String groupKey = key.replace(keyArr[2], "group");
				String cronKey = key.replace(keyArr[2], "cron");
				String enable = key.replace(keyArr[2], "enable");

				// 判断任务是否启用
				if (!Boolean.valueOf(properties.getProperty(enable))) {
					continue;
				}

				String group = properties.getProperty(groupKey);
				String name = properties.getProperty(nameKey);
				String cron = properties.getProperty(cronKey);
				String clazz = properties.getProperty(jobClassKey);

				ScheduleJob scheduleJob = new ScheduleJob();
				scheduleJob.setName(name);
				scheduleJob.setGroup(group);
				scheduleJob.setCronExpression(cron);
				scheduleJob.setClassName(clazz);
				JobKey jobkey = new JobKey(name, group);
				JobDetail detail = scheduleJobUtils.getJob(name, group);
				if (ToolUtil.isNotEmpty(detail)) {
					continue;
				}
				scheduleJobUtils.add(scheduleJob);
			}
		}
	}

	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}

	public String getJobs() {
		return jobs;
	}

	public void setJobs(String jobs) {
		this.jobs = jobs;
	}

	public static boolean isDsAlone() {
		return dsAlone;
	}

	public static void setDsAlone(boolean dsAlone) {
		QuartzPlugin.dsAlone = dsAlone;
	}
}
